$(function () {
    $.when(getdatadictionary("information_type"), getdatadictionary("voltage_type"), getdatadictionary("buy_type")).done($("#jqGrid").jqGrid({
        url: baseURL + 'api/bmaterial/list',
        datatype: "json",
        colModel: [
            {label: 'guid', name: 'guid', index: 'guid', width: 50, key: true, hidden: true},
            {   hidden: true,
                label: '信息库类别',
                name: 'mInformationType',
                index: 'm_information_type',
                width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in information_type) {
                        if (cellvalue == information_type[d].dKey) return information_type[d].dValue;
                    }
                }
            },
            {label: '物质编码', name: 'mCode', index: 'm_code', width: 80},
            {hidden: true,label: '材料大类', name: 'mBigtype', index: 'm_bigtype', width: 80},
            {label: '材料中类', name: 'mMidtype', index: 'm_midtype', width: 80},
            {hidden: true,label: '材料标签', name: 'mLabel', index: 'm_label', width: 80},
            {label: '材料名称', name: 'mName', index: 'm_name', width: 400},
            {hidden: true,label: '技术参数', name: 'mParameter', index: 'm_parameter', width: 80},
            {hidden: true,
                label: '电压等级', name: 'mVoltage', index: 'm_voltage', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in voltage_type) {
                        if (cellvalue == voltage_type[d].dKey)
                            return voltage_type[d].dValue;
                    }
                    return "";
                }
            },
            {label: '预算参数名称', name: 'mBParameter', index: 'm_b_parameter', width: 80},
            {label: '预算值', name: 'mQuery', index: 'm_query', width: 80},
            {hidden: true,
                label: '采购级别', name: 'mBuyType', index: 'm_buy_type', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in buy_type) {
                        if (cellvalue == buy_type[d].dKey) return buy_type[d].dValue;
                    }
                }
            },
            {label: '购置单位', name: 'mUnit', index: 'm_unit', width: 80},
            {label: '购置单价', name: 'mPrice', index: 'm_price', width: 80},
            {hidden: true,label: '单重（kg）', name: 'mWeight', index: 'm_weight', width: 80},
            {label: '是否主材', name: 'mIsmain', index: 'm_ismain', width: 80},

            {label: '配置预算项', name: 'mIshave', index: 'm_ishave', width: 80},



            {label: '预算项目分类', name: 'mClass', index: 'm_class', width: 80},
            {label: '预算项目', name: 'mItem', index: 'm_item', width: 80},
            {
                label: '操作', name: '', width: 180, formatter: function (cellvalue, options, rowObject) {
                console.log(rowObject)

                return "<a  class='btn btn-primary' onclick='show(\"" + rowObject.guid + "\"" +
                    ",\"" + rowObject.mUnit + "\"" +
                    ",\"" + rowObject.mClass + "\"" +
                    ",\"" + rowObject.mItem + "\"" +
                    ",\"" + rowObject.mQuery + "\",1)' >" +
                    "<i class='fa  fa-align-justify'></i>&nbsp;材料工程量</a>"
                }
            }
        ],
        viewrecords: true,
        height: "100%",
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    }));
});
function show(guid, mUnit, mClass, mItem, mQuery, i) {
    var title = "材料工程量";

    var content = "bmaterialquantities.html?aguid=" + guid + "&mUnit=" + mUnit + "&mClass=" + mClass + "&mItem=" + mItem + "&mQuery=" + mQuery;

    layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['90%', '90%'],
        content: content

    });
    layer.open({
        type: 2,
        title: '预算项',
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['90%', '90%'],
        content: '' + txt
    });
}
var information_type;
var voltage_type;
var buy_type;
function getdatadictionary(sss) {
    $.get(baseURL + "api/bdatadictionary/select?d_code=" + sss, function (r) {
        console.log(r.returnlist)
        if (sss == "information_type") {
            information_type = r.returnlist;
        } else if (sss == "voltage_type") {
            voltage_type = r.returnlist;
        } else if (sss == "buy_type") {
            buy_type = r.returnlist;
        }
    });
}
var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        bMaterial: {},
        information_type: {},
        voltage_type: {},
        buy_type: {},
        status: ["是", "否"],
        q: {
            mCode: null,
            mLabel: null,
            mIsmain: null,
            mIshave: null,

            mName: null
        }

    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.bMaterial = {};
            vm.information_type = information_type;
            vm.voltage_type = voltage_type;
            vm.buy_type = buy_type;
        },
        update: function (event) {
            var guid = getSelectedRow();
            if (guid == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.information_type = information_type;
            vm.voltage_type = voltage_type;
            vm.buy_type = buy_type;
            vm.getInfo(guid)
        },
        saveOrUpdate: function (event) {
            var url = vm.bMaterial.guid == null ? "api/bmaterial/save" : "api/bmaterial/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.bMaterial),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var guids = getSelectedRows();
            if (guids == null) {
                return;
            }

            confirm('确定要删除选中的记录？并且同时删除材料对应的工程量？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "api/bmaterial/delete",
                    contentType: "application/json",
                    data: JSON.stringify(guids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (guid) {
            $.get(baseURL + "api/bmaterial/info/" + guid, function (r) {
                vm.bMaterial = r.bMaterial;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {
                    'mCode': vm.q.mCode,
                    'mLabel': vm.q.mLabel,
                    'mIsmain': vm.q.mIsmain,
                    'mIshave': vm.q.mIshave,
                    'mName': vm.q.mName

                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});